﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RuoVea.ExFilter;

namespace RuoVea.Universal.Entry.Controllers;

/// <summary>
/// BaseApiController
/// </summary>
[ApiController]
[Route("api")]
[ApiExplorerSettings(GroupName = "v0")]
public class BaseApiController : ControllerBase
{
    private readonly ILogger<BaseApiController> _logger;

    /// <summary>
    /// BaseApiController
    /// </summary>
    /// <param name="logger"></param>
    public BaseApiController(ILogger<BaseApiController> logger)
    {
        _logger = logger;
    }

    /// <summary>
    /// 获取当前API版本
    /// </summary>
    /// <returns></returns>
    [HttpGet("version")]
    public Dictionary<string, string> Version()
    {
        var dict = new Dictionary<string, string>();
        var mainAssembly = System.Reflection.Assembly.GetEntryAssembly();
        var basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        dict["name"] = mainAssembly.FullName;
        dict["version"] = mainAssembly.GetName().Version.ToString();
        dict["publishTime"] = new FileInfo(Path.Combine(basePath, "RuoVea.Universal.Entry.dll")).LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
        dict["runningTime"] = (DateTime.Now - new FileInfo(Path.Combine(basePath, "RuoVea.Universal.Entry.dll")).LastWriteTime).ToString();
        dict["serverTime"] = (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds.ToString();

        return dict;
    }


    /// <summary>
    /// Swagger登录检查 🔖
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    [NonRestfulResult]
    [HttpPost("/api/swagger/checkUrl")]
    public int SwaggerCheckUrl()
    {
        return 200;// _httpContextAccessor.HttpContext.User.Identity.IsAuthenticated ? 200 : 401;
    }

    /// <summary>
    /// Swagger登录提交 🔖
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    /// <returns></returns>
    [AllowAnonymous]
    [NonRestfulResult]
    [HttpPost("/api/swagger/submitUrl")]
    public int SwaggerSubmitUrl([FromForm] string userName, [FromForm] string password)
    {
        try
        {
            return 200;
        }
        catch (Exception)
        {
            return 401;
        }
    }
}
